/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package newpackage;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenContext;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

/**
 *
 * @author Nigga
 */
@MessageDriven(mappedName = "jms/bean", activationConfig = {
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
    @ActivationConfigProperty(propertyName = "destinationLookup", propertyValue = "jms/QueueSample"),
    @ActivationConfigProperty(propertyName = "bean", propertyValue = "mBean")
})
public class NewMessageBean implements MessageListener {

    @Resource
    public MessageDrivenContext mdc;

    private String url;
    private String user;
    private String password;
    private PreparedStatement preparedStatement;

    public NewMessageBean() {
    }

    @Override
    public void onMessage(Message msg) {
        try {
            TextMessage textMessage = (TextMessage) msg;
            Class.forName("oracle.jdbc.driver.OracleDriver");
            url = "jdbc:oracle:thin:@localhost:1521:XE";
            user = "Lysergid";
            password = "19921992";
            try (Connection connection = DriverManager.getConnection(url, user, password)) {
                preparedStatement = connection.prepareStatement("insert into messages(time,text) values (TO_DATE((?),'DD-MM-YYYY HH24:MI:SS'),(?))");
                preparedStatement.setString(1, textMessage.getStringProperty("Time"));
                preparedStatement.setString(2, textMessage.getText());
                preparedStatement.execute();
                connection.close();
            }
        } catch (SQLException | JMSException | ClassNotFoundException ex) {
            Logger.getLogger(NewMessageBean.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}
